import chat from '../model/chat.js';
import { Op } from 'sequelize';

export async function addChatMessage(sender, receiver, message, date) {
  return await chat.create({
    sender,
    receiver,
    message,
    date,
    isRead: 0,
  });
}

export async function queryChatHistory(sender, receiver) {
  return await chat.findAll({
    where: {
      [Op.or]: [
        {
          sender,
          receiver,
        },
        {
          sender: receiver,
          receiver: sender,
        },
      ],
    },
    order: [['date', 'DESC']],
    // limit: 10,
  });
}

export function toRead(sender, receiver) {
  return chat.update(
    {
      isRead: 1,
    },
    {
      where: {
        sender,
        receiver,
      },
    },
  );
}
